package com.tianquan.DistributedLock;/**
 * @author starsea
 * @date 2024-09-25 上午8:33
 */

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.DistributedLock
 * @Author: tianquan
 * @CreateTime: 2024-09-25  08:33
 * @Description: 测试
 * @Version: 1.0
 */

@RestController
@RequiredArgsConstructor
public class LockController {

    public static int count = 0;

    public static int count1 = 0;

    private final DataBaseLock dataBaseLock;

    @GetMapping("/add")
    public void lock() throws InterruptedException {

        try {
            // 加锁
            dataBaseLock.lock("add");
            dataBaseLock.lock("add");
            // 业务逻辑
            count++;
            System.out.println("------------add count------------ = " + count);
            Thread.sleep(1000);
        } finally {
            // 释放锁
            dataBaseLock.unLock("add");
            dataBaseLock.unLock("add");
        }

    }


    @GetMapping("/add1")
    public void lock1() throws InterruptedException {

        try {
            // 加锁
            dataBaseLock.lock("add1");
            dataBaseLock.lock("add1");
            // 业务逻辑
            count1++;
            System.out.println("------------add1 count------------ = " + count);
            Thread.sleep(1000);
        } finally {
            // 释放锁
            dataBaseLock.unLock("add1");
            dataBaseLock.unLock("add1");
        }

    }

}
